var func = {};
func.init = function() {
    $.each(cpFunctions, function() {
        if (this.type == 1) {
            $("tr[ref='" + this.uri + "']").css({"display": "none"});
        }
    });
};

func.map = function(_function) {
    ajax({
        service: 'cpservice/function/getbycpfunction.json',
        data: {cpFunction: _function},
        type: 'post',
        done: function(resp) {
            if (resp.success) {
                popup.open("popup-cpfunction", "Định nghĩa quyền", template('/cp/tpl/function/map.tpl', resp), [
                    {
                        title: 'Map',
                        style: 'btn-success',
                        fn: function() {
                            var form = {};
                            form.name = $("input[name=name]").val();
                            form.groupId = $("select[name=groupId]").val();
                            form.groupName = $("input[name=groupName]").val();
                            form.groupPosition = $("input[name=groupPosition]").val();
                            form.uri = $("input[name=uri]").val();
                            form.position = $("input[name=position]").val();
                            form.actions = [];
                            $.each($("tr[for=service]"), function(index) {
                                var action = {};
                                action.uri = $(this).find("input[for=uri]").val();
                                action.name = $(this).find("input[for=name]").val();
                                action.position = $(this).find("input[for=position]").val();
                                action.skip = $(this).find("input[for=skip]").is(":checked");
                                form.actions[index] = action;

                            });
                            ajax({
                                service: 'cpservice/function/map.json',
                                data: form,
                                type: 'post',
                                contentType: 'json',
                                done: function(resp) {
                                    if (resp.success) {
                                        popup.msg(resp.message, function() {
                                            location.reload();
                                        });
                                    } else {
                                        var error = '<p>' + resp.message + '</p>';
                                        error += '<ul>';
                                        $.each(resp.data, function(key, value) {
                                            error += '<li>' + value + '</li>';
                                        });
                                        error += '</ul>';
                                        popup.msg(error);
                                    }
                                }
                            });
                        }
                    },
                    {
                        title: 'Đóng',
                        style: 'btn-default',
                        fn: function() {
                            popup.close('popup-cpfunction');
                        }
                    }
                ]);
            } else {
                popup.msg(resp.message);
            }
        }
    });
};

func.removeCpFunctionGroup = function(_groupId) {
    popup.confirm("Bạn chắc chắn muốn xóa nhóm quyền này ?", function() {
        ajax({
            service: 'cpservice/function/removegroup.json',
            data: {id: _groupId},
            done: function(resp) {
                if (resp.success) {
                    popup.msg(resp.message, function() {
                        location.reload();
                    });
                } else {
                    popup.msg(resp.message);
                }
            }
        });
    });
};

func.changeCpFunctionGroup = function(_groupId) {
    ajax({
        service: 'cpservice/function/getcpfunctiongroup.json',
        data: {id: _groupId},
        done: function(resp) {
            if (resp.success) {
                popup.open("popup-cpfunction", "Chi tiết nhóm quyền", template('/cp/tpl/function/groupfunction.tpl', resp), [
                    {
                        title: 'Map',
                        style: 'btn-success',
                        fn: function() {
                            ajaxSubmit({
                                service: 'cpservice/function/editcpfunctiongroup.json',
                                id: 'form-edit',
                                contentType: 'json',
                                done: function(resp) {
                                    if (resp.success) {
                                        popup.msg(resp.message, function() {
                                            location.reload();
                                        });
                                    } else
                                        popup.msg(resp.message);
                                }
                            });
                        }
                    },
                    {
                        title: 'Đóng',
                        style: 'btn-default',
                        fn: function() {
                            popup.close('popup-cpfunction');
                        }
                    }
                ]);
            } else {
                popup.msg(resp.message);
            }
        }
    });

};

func.changeCpFunction = function(_id) {
    ajax({
        service: 'cpservice/function/getcpfunction.json',
        data: {id: _id},
        done: function(resp) {
            if (resp.success) {
                var rdata = resp.data;
                var data = new Object();
                data.cpFunc = resp.data.cpFunction.uri;
                data.name = resp.data.cpFunction.name;
                data.position = resp.data.cpFunction.position;
                data.groupId = resp.data.cpFunction.groupId;
                data.action = resp.data.actions;
                resp.data = data;
                popup.open("popup-function", "Chi tiết chức năng " + resp.data.name, template('/cp/tpl/function/map.tpl', resp), [
                    {
                        title: 'Cập nhật',
                        style: 'btn-info',
                        fn: function() {
                            var form = {};
                            form.name = $("input[name=name]").val();
                            form.groupId = $("select[name=groupId]").val();
                            form.groupName = $("input[name=groupName]").val();
                            form.groupPosition = $("input[name=groupPosition]").val();
                            form.uri = $("input[name=uri]").val();
                            form.position = $("input[name=position]").val();
                            form.actions = [];
                            $.each($("tr[for=service]"), function(index) {
                                var action = {};
                                action.uri = $(this).find("input[for=uri]").val();
                                action.name = $(this).find("input[for=name]").val();
                                action.position = $(this).find("input[for=position]").val();
                                action.skip = $(this).find("input[for=skip]").is(":checked");
                                form.actions[index] = action;

                            });
                            ajax({
                                service: 'cpservice/function/map.json',
                                data: form,
                                type: 'post',
                                contentType: 'json',
                                done: function(resp) {
                                    if (resp.success) {
                                        popup.msg(resp.message, function() {
                                            location.reload();
                                        });
                                    } else {
                                        var error = '<p>' + resp.message + '</p>';
                                        error += '<ul>';
                                        $.each(resp.data, function(key, value) {
                                            error += '<li>' + value + '</li>';
                                        });
                                        error += '</ul>';
                                        popup.msg(error);
                                    }
                                }
                            });
                        }
                    },
                    {
                        title: 'Đóng',
                        style: 'btn-default',
                        fn: function() {
                            popup.close('popup-function');
                        }
                    }
                ]);

                setTimeout(function() {
                    $.each(rdata.cpFunction.actions, function() {
                        var action = this;
                        $.each($("tr[for=service]"), function() {
                            if (action.uri === $(this).find("input[for=uri]").val()) {
                                $(this).find("input[for=name]").val(action.name);
                                $(this).find("input[for=position]").val(action.position);
                                if (action.skip == 0) {
                                    $(this).find("input[for=skip]").attr("checked", false);
                                } else {
                                    $(this).find("input[for=skip]").attr("checked", true);
                                }
                            }
                        });
                    });
                }, 300);
            } else {
                popup.msg(resp.message);
            }
        }
    });
};

func.removeCpFunction = function(_id) {
    popup.confirm("Bạn chắc chắn muốn xóa chức năng này ?", function() {
        ajax({
            service: 'cpservice/function/removefunction.json',
            data: {id: _id},
            done: function(resp) {
                if (resp.success) {
                    popup.msg(resp.message, function() {
                        location.reload();
                    });
                } else {
                    popup.msg(resp.message);
                }
            }
        });
    });
};